﻿using DevExpress.Utils;
using DevExpress.XtraEditors;
using DevExpress.XtraEditors.Controls;
using DevExpress.XtraEditors.Repository;
using DLL;
using SQLDLL;
using System;
using System.Data;
using System.Windows.Forms;

namespace NursingFormSystem
{
    public partial class ChangeHealthEducation : FormBase
    {
        bool notedu = false;
        string neirong = "";
        public string id = "";
        public ChangeHealthEducation(string[] UserParam, string PATIENT_ID, string VISIT_ID, string DiseasesGuid, string ProjectGuid, string ClassGuid, string ID)
        {
            id = ID;
            this.UserParam = UserParam;
            base.InitLoginUser(UserParam);
            InitializeComponent();
            string FirstSelect = @"SELECT * FROM [BNNursingDocsSystem_Develop].[dbo].[V_YDHL_PATIENT](NOLOCK) where  NURSE_CELL_CODE = '" + UserParam[2] + "' and PATIENT_ID ='" + PATIENT_ID + "' and VISIT_ID = '" + VISIT_ID + "'";
           DataSet ds = SqlHelper.ExecuteDataset(FirstSelect, CommandType.Text);
            if (ds == null || ds.Tables.Count <= 0) return;
            DataTable Patients = ds.Tables[0];

            // 1男 2女 值可能是数字或者中文或者空
            string Sex = string.Empty;//默认
            if (Patients == null || Patients.Rows.Count <= 0 || Patients.Rows[0]["SEX"] == DBNull.Value || Patients.Rows[0]["SEX"] == null) Sex = string.Empty;
            string data_sex = Patients.Rows[0]["SEX"].ToString().Trim();
            if (string.IsNullOrEmpty(data_sex)) Sex = string.Empty;
            Sex = data_sex;
            if (data_sex.Equals("1")) Sex = "男";
            if (data_sex.Equals("2")) Sex = "女";

            if (Patients == null || Patients.Rows.Count <= 0)
                return;

            string PatientID = Patients.Rows[0]["PATIENT_ID"].ToString().Trim();
            string InDate = Convert.ToDateTime(Patients.Rows[0]["IN_DATE"].ToString().Trim()).ToString("yyyy-MM-dd");
            string VisitID = Patients.Rows[0]["VISIT_ID"].ToString().Trim();
            labelName.Text = Patients.Rows[0]["PATIENT_NAME"].ToString().Trim();
            labelPatientID.Text = PatientID;
            labelSex.Text = Sex;
            labelVisitID.Text = VisitID;
            labelInDate.Text = InDate;
            labelBed.Text = Patients.Rows[0]["BED_NO"].ToString().Trim();

            string SecondSelect = @"SELECT * FROM [BNNursingDocsSystem_Develop].[dbo].[HealthEduBase](NOLOCK) where id = '" + id + "' ";
            DataTable Info = SqlHelper.ExecuteDataset(SecondSelect, CommandType.Text).Tables[0];
            comboBoxTemplateName.Text = Info.Rows[0]["TemplateName"].ToString();
            comboxDiseasesName.Text = Info.Rows[0]["DiseasesName"].ToString();
            comboBoxClassName.Text = Info.Rows[0]["ClassName"].ToString();
            dateEdit1.Properties.VistaEditTime = DefaultBoolean.True;  //加钟
            if (Info.Rows[0]["EduDate"].ToString().Length > 0)
            {
                dateEdit1.Text = Info.Rows[0]["EduDate"].ToString();
                labelControl17.Text = "";
            }
            else
            {
                dateEdit1.Text = System.DateTime.Now.ToString("yyyy-MM-dd HH:mm");
                labelControl17.Text = "暂无宣教时间";
            }
            neirong = Info.Rows[0]["ProjectContent"].ToString();
            comboBoxEdit1.Text = Info.Rows[0]["EduObj"].ToString();
            comboBoxEdit2.Text = Info.Rows[0]["EduMethods"].ToString();
            comboBoxEdit3.Text = Info.Rows[0]["Evaluation"].ToString();
            labelControl12.Text = neirong;
            comboBoxEdit4.Properties.Items.Clear();
            comboBoxProjectGuid.Properties.Items.Clear();

            string SixthSelect = @"SELECT *  FROM [BNNursingDocsSystem_Develop].[dbo].[HealthEduProject](NOLOCK) where ClassGuid = '" + ClassGuid + "'";
            DataTable Project = SqlHelper.ExecuteDataset(SixthSelect, CommandType.Text).Tables[0];
            string[] ProjectName = new string[Project.Rows.Count];
            string[] ProjectGuidList = new string[Project.Rows.Count];
            for (int i = 0; i < Project.Rows.Count; i++)
            {
                ProjectName[i] = Project.Rows[i]["ProjectName"].ToString().Trim();
                ProjectGuidList[i] = Project.Rows[i]["ProjectGuid"].ToString().Trim();  //ProjectGuid为内容id
            }
            ComboBoxItemCollection coll = comboBoxEdit4.Properties.Items;
            coll.BeginUpdate();
            ComboBoxItemCollection coll2 = comboBoxProjectGuid.Properties.Items;
            coll2.BeginUpdate();
            for (int j = 0; j < ProjectName.Length; j++)
            {
                coll.Add(ProjectName[j]);
                coll2.Add(ProjectGuidList[j]);
            }
            coll.EndUpdate();
            coll2.EndUpdate();
            Controls.Add(comboBoxEdit4);
            Controls.Add(comboBoxProjectGuid);
            for (int i = 0; i < ProjectName.Length; i++)
            {
                if (ProjectGuid == ProjectGuidList[i])
                {
                    comboBoxEdit4.SelectedIndex = i;
                    comboBoxProjectGuid.SelectedIndex = i;
                }
            }
        }

        public string[] UserParam { get; set; }

        private void comboBoxEdit4_SelectedIndexChanged(object sender, EventArgs e)
        {
            comboBoxProjectGuid.SelectedIndex = comboBoxEdit4.SelectedIndex;
            string sql = @"SELECT  ProjectName as '项目', ProjectContent as '内容',EduObj as '宣教对象', EduMethods as '宣教方式',Evaluation as '评价', DiseasesGuid,ProjectGuid,ClassGuid,EduDate FROM [BNNursingDocsSystem_Develop].[dbo].[HealthEduBase](NOLOCK) where PatientID = '" + labelPatientID.Text + "  ' and VisitID = '" + labelVisitID.Text + "' and ProjectGuid = '" + comboBoxProjectGuid.Text + "'";
            DataTable BaseTemp = SqlHelper.ExecuteDataset(sql, CommandType.Text).Tables[0];
        }

        private void simpleButton2_Click(object sender, EventArgs e)
        {
            Close();
        }

        private void simpleButton1_Click(object sender, EventArgs e)
        {
            if (!notedu)
            {
                string EduObj = comboBoxEdit1.Text;
                string EduMethods = comboBoxEdit2.Text;
                string Evaluation = comboBoxEdit3.Text;
                string sql = @"use BNNursingDocsSystem_Develop ; update HealthEduBase set [EduDate] = '" + dateEdit1.Text + "',[EduObj] = '" + EduObj + "',[EduMethods] = '" + EduMethods + "',[Evaluation] = '" + Evaluation + "',[EvaluationDate] = '" + dateEdit1.Text + "'where PatientID = '" + labelPatientID.Text + "  ' and VisitID = '" + labelVisitID.Text + "' and ProjectGuid = '" + comboBoxProjectGuid.Text + "' and id ='" + id + "'";
                SqlHelper.ExecuteNonQuery(CommandType.Text, sql);
                XtraMessageBox.Show("保存成功");
                comboBoxEdit4_SelectedIndexChanged(null, null);
                Close();
            }
            else
            {
                simpleButton2_Click(null, null);
            }
        }
    }
}